package com.csun.cmny.provider.utils;

import com.csun.cmny.util.RedisKeyUtil;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class SerialNumberUtil {

    @Resource
    private RedisTemplate redisTemplate;

    public String getSerialNumberString() {

        String today = DateUtil.getDateString();
        String serialnumberKey = RedisKeyUtil.getOmcRechargeSerialnumber(today);

        if (!redisTemplate.hasKey(serialnumberKey)) {
            redisTemplate.opsForValue().set(serialnumberKey, 0, 1, TimeUnit.DAYS);
        }

        Long number = redisTemplate.opsForValue().increment(serialnumberKey, 1);

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("SHJ").append(today).append("00000");

        int start = stringBuffer.length() - number.toString().length();
        int end = stringBuffer.length() + 1;

        return stringBuffer.replace(start, end, number.toString()).toString();
    }
}
